package gson.emlog.activity;

import gson.emlog.R;
import gson.emlog.adapter.TwitterAdapter;
import gson.emlog.utils.Action;
import gson.emlog.utils.Http;
import gson.emlog.utils.Result;

import java.util.HashMap;

import org.json.simple.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 碎语
 * 
 * @author GodSon
 * 
 */
public class Twitter extends Activity {

	private ListView twview;
	Button newBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twitter);

		twview = (ListView) findViewById(R.id.twitterView);

		twview.setOnItemLongClickListener(ItemLongClic);
		twview.setOnItemClickListener(itemClick);

		newBtn = (Button) findViewById(R.id.twitternew);
		newBtn.setOnClickListener(newBtnOnClickListener);
		newBtn.setEnabled(false);
		Toast.makeText(Twitter.this, "加载中ing", Toast.LENGTH_SHORT).show();
		Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				Http http = new Http(Twitter.this);
				Result obj = http.post(Action.BLOG_TW);
				JSONArray data = new JSONArray();
				if (obj.getStatus()) {
					data = (JSONArray) obj.getData();
				}else{
					Toast.makeText(Twitter.this, obj.getData().toString(), Toast.LENGTH_SHORT).show();
				}

				TwitterAdapter tadpt = new TwitterAdapter(data, Twitter.this);

				twview.setAdapter(tadpt);
				newBtn.setEnabled(true);
			}
		};
		handler.postDelayed(runnable, 500);

	}

	private View.OnClickListener newBtnOnClickListener = new View.OnClickListener() {

		public void onClick(View v) {
			Builder alert = new AlertDialog.Builder(Twitter.this);
			final EditText tucao = new EditText(Twitter.this);
			tucao.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
			tucao.setSingleLine(false);
			tucao.setGravity(Gravity.TOP);
			tucao.setMaxLines(5);
			alert.setTitle(getResources().getString(R.string.tucao));
			alert.setView(tucao);
			alert.setPositiveButton("确认", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Http http = new Http(Twitter.this);
					HashMap<String, Object> parmas = new HashMap<String, Object>();
					String contents = tucao.getText().toString();
					parmas.put("c", contents);
					Result obj = http.post(Action.BLOG_ADD_TW,parmas);
					if(obj.getStatus()){
						Toast.makeText(Twitter.this, "吐槽成功！", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(Twitter.this, obj.getData().toString(), Toast.LENGTH_SHORT).show();
					}
				}
			});
			alert.setNegativeButton("取消", null);
			alert.show();
		}
	};
	
	private AdapterView.OnItemClickListener itemClick = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adptv, View v, int position,
				long id) {
			TwitterAdapter adpt = (TwitterAdapter) adptv.getAdapter();
			int count = adpt.getReplyCount(position);

			if (count > 0) {
				//startActivity(new Intent(Twitter.this, TwitterReply.class));
			}
		}
	};
	
	
	private AdapterView.OnItemLongClickListener ItemLongClic = new OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> adptv, View v,
				int position, long id) {
			
			return true;
		}
	};
}
